import {basePath, authorization} from "./client.js";
import {json} from "react-router-dom";

let path = basePath + '/medicine-list'

// 获取medicine list item,根据 order id
export const getMedicineItems = async (id) => {
    const res = await fetch(path + `/dto/find/${id}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

// 获取medicine list item,根据 medicine history id
export const getMedicineItemsByMHid = async (id) => {
    const res = await fetch(path + `/dto/find/mh/${id}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

/**
 * 根据id 将药品加入药单
 * @param mid 药品id
 * @param lid 药单id
 * @param number 药品数量
 * @returns {Promise<any>}
 */
export const addMedicine2List = async ({mid, oid, number}) => {
    const res = await fetch(path + `/dto/save`, {
        body: JSON.stringify({mid, oid, number}),
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'POST',
        mode: 'cors'
    })
    return res.json()
}

/**
 * 更新药单item的数量
 * @param mid
 * @param number
 * @returns {Promise<any>}
 */
export const uptMedicineListItemNum = async ({mid, number}) => {
    const res = await fetch(path + `/dto/save`, {
        body: JSON.stringify({mid, name, number}),
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'PUT',
        mode: 'cors'
    })
    return res.json()
}

/**
 * 开单完毕, 设置order状态为2(已开), 设置药单总价格
 * @param orderId
 * @param price
 * @returns {Promise<any>}
 */
export const medicineListOver = async ({orderId, price}) => {
    const res = await fetch(path + `/dto/over`, {
        body: JSON.stringify({orderId, price}),
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'PUT',
        mode: 'cors'
    })
    return res.json()
}

/**
 * 通过学生id获取药单list
 * @param studentId
 * @returns {Promise<any>}
 */
export const listByStudentId = async (studentId) => {
    const res = await fetch(path + `/student/${studentId}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'GET',
        mode: 'cors'
    })
    return res.json()
}

/**
 * 更新药单id为已拿
 * @returns {Promise<any>}
 */
export const payMedicineHistory = async (mhid) => {
    const res = await fetch(path + `/pay/${mhid}`, {
        headers: {
            'content-type': 'application/json',
            'authorization': authorization
        },
        method: 'PUT',
        mode: 'cors'
    })
    return res.json()
}